﻿using System;
using System.Configuration;
using System.Diagnostics.CodeAnalysis;

namespace Fuse8.DomainFramework.Configuration.Cache
{
    /// <summary>
    /// 
    /// </summary>
    [SuppressMessage("Microsoft.Design", "CA1010:CollectionsShouldImplementGenericInterface",
        Justification = "This is ConfigurationElementCollection")]
    public class CacheContainersCollection : ConfigurationElementCollection
    {
        #region ConfigurationElementCollection members

        /// <summary>
        /// 
        /// </summary>
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.AddRemoveClearMap;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new CacheContainerElement();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="elementName"></param>
        /// <returns></returns>
        protected override ConfigurationElement CreateNewElement(string elementName)
        {
            return new CacheContainerElement(elementName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            return (element as CacheContainerElement).Name;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public CacheContainerElement this[int index]
        {
            get
            {
                return BaseGet(index) as CacheContainerElement;
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }

                BaseAdd(index, value);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public new CacheContainerElement this[string name]
        {
            get
            {
                return BaseGet(name) as CacheContainerElement;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public int IndexOf(CacheContainerElement element)
        {
            return BaseIndexOf(element);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="element"></param>
        public void Add(CacheContainerElement element)
        {
            BaseAdd(element);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="element"></param>
        public void Remove(CacheContainerElement element)
        {
            if (BaseIndexOf(element) >= 0)
            {
                BaseRemove(element.Name);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        public void Remove(string name)
        {
            BaseRemove(name);
        }

        /// <summary>
        /// 
        /// </summary>
        public void Clear()
        {
            BaseClear();
        }

        #endregion
    }
}
